//
//  RootViewController.m
//  UI21_动画
//
//  Created by dllo on 15/12/8.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@property (nonatomic, retain) UIImageView *imageV;

@end

@implementation RootViewController

- (void)dealloc
{
    [_imageV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
    self.imageV.image = [UIImage imageNamed:@"diamond"];
    [self.view addSubview:self.imageV];
    [_imageV release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 300, 100, 100);
    button.backgroundColor = [UIColor cyanColor];
    
    // layer
    // 阴影颜色
    button.layer.shadowColor = [[UIColor blackColor] CGColor];
    // 阴影偏移
    button.layer.shadowOffset = CGSizeMake(5, 5);
    // 阴影透明度
    button.layer.shadowOpacity = 0.5;
    // 阴影模糊程度
    button.layer.shadowRadius = 0.5;
    
    // 圆角 - 若要变成圆形,需要宽度=高度并且圆角设置为宽度的一半
//    button.layer.cornerRadius = 50;
    
    // 描边颜色
    button.layer.borderColor = [[UIColor redColor] CGColor];
    // 描边宽度
    button.layer.borderWidth = 1.5;
    
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)buttonAction:(UIButton *)sender
{

//    [UIView animateWithDuration:2.f animations:^{
//       
//        // 延时
//        [UIView setAnimationDelay:1.f];
//        
//        // 速度曲线
//        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//        
//        // 动画反转
//        [UIView setAnimationRepeatAutoreverses:YES];
//        
//        // 重复次数
//        [UIView setAnimationRepeatCount:3];
//        
//        self.imageV.frame = CGRectMake(250, 500, 100, 100);
//        
//        
//    }];

//    [UIView animateWithDuration:1.f animations:^{
//        
//        self.imageV.frame = CGRectMake(300, 500, 100, 100);
//        
//    } completion:^(BOOL finished) {
//        
//        // 动画完成后可以加一些处理
//        self.imageV.frame = CGRectMake(0, 0, 50, 50);
//        
//    }];

//    [UIView animateWithDuration:1.f delay:1.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
//        
//        self.imageV.frame = CGRectMake(300, 500, 100, 100);
//        
//    } completion:^(BOOL finished) {
//        
//        self.imageV.frame = CGRectMake(0, 0, 50, 50);
//        
//    }];
    
//    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 300, 50, 50)] autorelease];
//    view.backgroundColor = [UIColor greenColor];
//    
//    [UIView transitionFromView:self.imageV toView:view duration:1.f options:UIViewAnimationOptionCurveEaseInOut completion:^(BOOL finished) {
//        
//        
//    }];

    CABasicAnimation *basicA = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    basicA.fromValue = @(1);
    basicA.toValue = @(3);
    basicA.duration = 3.f;
    basicA.autoreverses = YES;
    basicA.repeatCount = NSIntegerMax;
    
//    [self.imageV.layer addAnimation:basicA forKey:@"aaa"];
    
    CABasicAnimation *basicA1 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    basicA1.fromValue = @(0);
    basicA1.toValue = @(2 *M_PI);
    basicA1.duration = 3.f;
    basicA1.autoreverses = YES;
    basicA1.repeatCount = NSIntegerMax;
    
//    [self.imageV.layer addAnimation:basicA1 forKey:@"bbb"];
    
    CAAnimationGroup *groupA = [CAAnimationGroup animation];
    groupA.animations = @[basicA, basicA1];
    
    // 一部分参数会覆盖原有参数
    groupA.duration = 3.f;
    groupA.repeatCount = NSIntegerMax;
    groupA.autoreverses = YES;
    
//    [self.imageV.layer addAnimation:groupA forKey:@"ccc"];
    
    
    // 帧动画
    CAKeyframeAnimation *keyframeA = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    // 创建可变路径
    CGMutablePathRef path = CGPathCreateMutable();
    
    // 设置轨迹
    CGPathMoveToPoint(path, NULL, 50, 50);
    
    CGPathAddLineToPoint(path, NULL, 100, 100);
    CGPathAddLineToPoint(path, NULL, 250, 100);
    CGPathAddLineToPoint(path, NULL, 250, 250);
    CGPathAddLineToPoint(path, NULL, 100, 100);
    
    // 曲线
    // 需要设置3个点坐标
    // (cp1x, cp1y) (cp2x, cp2y) - 会尽量靠近这两个点但不会穿过
    // (x, y) - 目的点
    CGPathAddCurveToPoint(path, NULL, 50, 50, 300, 300, 200, 500);
    
    keyframeA.path = path;
    keyframeA.duration = 3;
    
//    [self.imageV.layer addAnimation:keyframeA forKey:@"ddd"];
    
    
    CATransition *tran = [CATransition animation];
    tran.type = @"cube";
    tran.subtype = kCATransitionFromLeft;
    
//    tran.duration = 2.f;
//    tran.repeatCount = NSIntegerMax;
    
    [sender.layer addAnimation:tran forKey:@"eee"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
